$(function() {
	$('#vue_vm_list div').hide();
});

var userRfidCard = {
	userRfidCardId: '',
	userRelationId: '',
	cardNumber: '',
	type: '',
	userType: '',
	price: '',
	payWay: '',
	transactStatus: '',
	status: '',
	createTime: '',
	updateTime: '',
	createId: getLoginedUser()? getLoginedUser().staffId: '',
	updateId: getLoginedUser()? getLoginedUser().staffId: '',
	remark: '',
	personName: '',
	userCardId: ''
};

var view = {
	data: userRfidCard
};

var vue_vm = new Vue({
	el: '#vue_vm',
	data: view
});

function save() {
	view.data.cardNumber = $('#cardNumber').val();
	view.data.type = $('#rfidType_select').val();
	view.data.price = (view.data.price==''? 0: parseFloat(view.data.price));
	view.data.transactStatus = (view.data.transactStatus==''? 0: parseInt(view.data.transactStatus));
	view.data.payWay = parseInt(view.data.payWay);
	
	if(view.data.userRelationId=='') {
		alert('请查询并选择对象');
		return;
	}
	
	if(view.data.cardNumber=='') {
		alert('请输入卡号');
		return;
	}
	
	if(view.data.type=='') {
		alert('请选择卡类型');
		return;
	}
	
	if(view.data.userRelationId=='') {
		alert('请查询并勾选对象');
		return;
	}
	
	
	if(view.data.price=='') {
		alert('请输入价格');
		return;
	}
	
	if(view.data.payWay=='') {
		alert('请选择支付方式');
		return;
	}

	save_basic(path+'/userRfidCard/saveHandle', {userRfidCard:view.data}, function() {
		alert('RFID卡办理成功');
	});
}

function findAll() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();

	findAll_basic(path+'/userRfidCard/findAll', function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			}
		});
		
		$('#vue_vm_list div').show();
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/userRfidCard/deleteBatch');
}

function unregister_batch() {
	var itemIdStr = '';
	var itemIdArray = [];
	
	$('ul').each(function() {
		if($(this).find('.magic-checkbox').is(':checked')) {
			var itemId = $(this).find('.magic-checkbox').attr('id');
			itemIdStr += (itemIdStr==''? itemId: ','+itemId);
			itemIdArray.push(itemId);
		}
	});
	
	if(itemIdArray.length == 0) {
		alert('请先勾选所要注销的项');
		return;
	}
	
	if(confirm('确实要注销所选项吗？')) {
		openWaiting('正在注销');
		
		var dataStr = JSON.stringify({ids:itemIdArray});
		
		$.post(path+'/userRfidCard/writeOffBatch', {data: dataStr}, function(response) {
			closeWaiting();
			
			if(response.code=='200') {
				find('userRfidCard/find');
				
				alert('注销成功');
			}else {
				alert(response.info);
			}
			
			console.log(response);
		});
	}
};

function findPerson() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	var searchVal = $('#searchVal').val();
	
	openWaiting('正在查找');
	
	find_basic(path+'/person/findPerson', searchVal, function(response) {
		closeWaiting();
		
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				onClickUserdRadiobox:function(todo) {
					var userRelationId = $('input:radio[name="userRelationId"]:checked').val();
					if(userRelationId==todo.personRelationId) {
						view.data.userRelationId = todo.personRelationId;
						view.data.userCardId = todo.cardId;
						view.data.personName = todo.name;
						view.data.userType = (todo.type=='长者'?1:2);
					}
				}
			}
		});
		
		$('#vue_vm_list div').show();
	});
}